package mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;
import java.util.List;

import static mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.MemberConstant.WEIXIN;
import static mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.MemberConstant.ZHIFUBAO;

/**
 * Created by 轻吻旧时光 on 2017/9/second.
 */

public class PackageUtil {

    private static AlertDialog alertDialog;

    /**
     * 判断 用户是否安装支付宝客户端
     */
    public static boolean isZhifubaoInstall(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }

    /**
     * 判断 用户是否安装微信客户端
     */
    public static boolean isWeixinInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断 用户是否安装qq客户端
     */
    public static boolean isQQInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq") || pn.equals("com.tencent.mqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void startMarket(Activity act, String packageName) {
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            act.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(act, "无法启动应用市场 !", Toast.LENGTH_SHORT).show();
        }
    }


    //是否安装客户端,传入包名即可
    public static boolean isInstall(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    //支付，判断是否安装客户端
    public static boolean PayisInstall(Context context, String type) {

        switch (type) {
            case WEIXIN:
                boolean b = isWeixinInstall(context);
                if (b) {
                    return true;
                } else {
                    showPoPWindow(context, type);
                    return false;
                }

            case ZHIFUBAO:
                boolean install = isZhifubaoInstall(context);
                if (install) {
                    return true;
                } else {
                    showPoPWindow(context, type);
                    return false;
                }
            default:
                return false;
        }
    }

    private static void showPoPWindow(final Context context, final String type) {
        if (alertDialog != null && alertDialog.isShowing()) {
            return;
        }
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("提示");
        switch (type) {
            case WEIXIN:
                dialog.setMessage("检测到您未安装微信客户端，是否去应用市场下载?");
                break;
            case ZHIFUBAO:
                dialog.setMessage("检测到您未安装支付宝客户端，是否去应用市场下载?");
                break;
            default:
                break;
        }

        dialog.setPositiveButton("去下载", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
                switch (type) {
                    case WEIXIN:
                        PackageUtil.startMarket((Activity) context, "com.tencent.mm");
                        break;
                    case ZHIFUBAO:
                        PackageUtil.startMarket((Activity) context, "com.eg.android.AlipayGphone");
                        break;
                    default:
                        break;
                }

            }
        });
        dialog.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
            }
        });
        alertDialog = dialog.create();
        alertDialog.setCancelable(false);
        alertDialog.show();
    }
}
